package cn.ydxiaoshuai.ai.chat.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ERNIEService
 * @description:
 * @author: 小帅丶
 * @create: 2025-09-19 13:41
 * @Version 1.0
 **/
@Configuration
public class ERNIEService {
    @Value("${spring.ai.openai.base-url}")
    private String ERNIE_BASE_URL;
    @Value("${spring.ai.openai.api-key}")
    private String ERNIE_KEY;

    @Bean
    public ChatClient ERNIEClient() {
       try {
           //1、创建指定模型的配置
           OpenAiChatOptions options = new OpenAiChatOptions();
           options.setTemperature(0.6);
           //options.setModel(ModelType.ERNIE_4_5_21B_A3B_Thinking.getModelName());
           options.setModel("default");
           // 构建深度思考所需的HTTP头（Map<String, String>类型）
           Map<String, String> ernieHeaders = new HashMap<>();
           options.setHttpHeaders(ernieHeaders);
           // 创建模型实例
           OpenAiChatModel model = new OpenAiChatModel(new OpenAiApi(ERNIE_BASE_URL, ERNIE_KEY), options);
           // 创建ChatClient
           return ChatClient
                   .builder(model)
                   .defaultAdvisors(
                           new SimpleLoggerAdvisor()
                   )
                   .build();
       }catch (Exception e){
           e.printStackTrace();
       }
       return null;
    }

}
